/*
 * @Author: ChunLai
 * @Date: 2023-07-26 10:33:07
 * @LastEditTime: 2025-11-24 14:30:11
 * @Description:
 * @FilePath: \01.latest-short-insure\src\store\modules\insert.js
 */
const userModel = {
  sex: "1",
  relationToAppnt: "",
  relationToAppntName: "",
  socialInsuFlag: "",
  name: "",
  idType: "",
  idName: "",
  idNum: "",
  validDate: "",
  validDateEnd: "",
  age: "",
  birthDate: "",
  phone: "",
  occupationCode: "",
  occupationCodeName: "",
  occupationClass: "",
  areaName: "",
  permanentAddress: "",
  province: "",
  height: "",
  weight: "",
  city: "",
  county: "",
  zipCode: "",
  frontImg: "",
  backImg: "",
  healthyStatus: "",
  uwMedicalId: "",
};

const state = {
  msChannel: "0",
  comCode: "", // "机构编码",
  underwriteFlag: "", // 核保状态
  insureFlag: "", // 保单状态
  insureMsg: "", // 保单提示
  forbidAlter: false, // 禁止修改信息
  applicant: {
    age: "18",
    IMPARTCOUNT: 1,
    IMPARTS: "",
    name: "",
    idType: "",
    idName: "",
    idNum: "",
    validDate: "",
    validDateEnd: "",
    sex: "1",
    birthDate: "",
    phone: "",
    email: "",
    occupationCode: "",
    occupationCodeName: "",
    occupationClass: "",
    socialInsuFlag: "",
    areaName: "",
    permanentAddress: "",
    province: "",
    city: "",
    county: "",
    zipCode: "",
    validCode: "",
    frontImg: "",
    backImg: "",
    crsIdTypeName: "",
  },
  bbrList: [
    {
      sex: "1",
      relationToAppnt: "4",
      relationToAppntName: "本人",
      socialInsuFlag: "0",
      name: "",
      idType: "",
      idName: "",
      idNum: "",
      validDate: "",
      validDateEnd: "",
      age: "",
      birthDate: "",
      phone: "",
      occupationCode: "",
      occupationCodeName: "",
      occupationClass: "",
      areaName: "",
      permanentAddress: "",
      province: "",
      height: "",
      weight: "",
      city: "",
      county: "",
      zipCode: "",
      frontImg: "",
      backImg: "",
      healthyStatus: "",
      uwMedicalId: "",
    },
  ],
  payInfo: {
    printPolType: "E", // "获取保单类型，E 电子保单，P 纸质保单",
    totalPrem: "", //"总保费",
    bankCode: "", //"银行编码",
    bankName: "", //"银行名称",
    bankAccName: "", //"持卡人户名",
    bankAccNo: "", //"银行卡号",
    firBankMobile: "", // "预留手机号",
    expayMode: "", //扣款方式： 微信、银行卡
    renewFlag: "",
    bnfBankCode: "",
    bindId: "",
    effectiveDate: "", //生效起期
    expiryDate: "", //生效止期
    authTransrNo: "",
    transactionId: "",
  },
  recall: "0", // 防止reBackRiskApiInfos方法无效调用
  bbrRefresh: false,
};
const getters = {
  msChannel: (state) => state.msChannel,
  forbidAlter: (state) => state.forbidAlter,
  underwriteFlag: (state) => state.underwriteFlag,
  recall: (state) => state.recall,
  insureFlag: (state) => state.insureFlag,
  insureMsg: (state) => state.insureMsg,
  applicant: (state) => state.applicant,
  payInfo: (state) => state.payInfo,
  totalPrem: (state) => state.payInfo.totalPrem,
  bbrRefresh: (state) => state.bbrRefresh,
  inputsConfig: (state, getters, rootState) => {
    return JSON.parse(JSON.stringify(rootState.infoReg.inputReg));
  },
  appnt: (state, getters, rootState) => {
    let objs = JSON.parse(JSON.stringify(getters.inputsConfig.applicant));
    for (const key in objs) {
      objs[key] = state.applicant[key];
    }
    if (state.applicant.age) {
      objs.age = state.applicant.age;
    }
    if (state.applicant.sex !== "") {
      objs.sex = state.applicant.sex;
    }
    if (state.applicant.birthDate) {
      objs.birthDate = state.applicant.birthDate;
    }
    return objs;
  },
  assuredList: (state, getters, rootState) => {
    // console.log("getters---2", state.bbrList);
    const apiObj = JSON.parse(JSON.stringify(getters.inputsConfig.insured));
    let list = [];
    const len = state.bbrList.length;
    for (let index = 0; index < len; index++) {
      const item = state.bbrList[index];
      let tar = JSON.parse(JSON.stringify(apiObj));
      if (item.relationToAppnt == "4") {
        let tbrObj = JSON.parse(JSON.stringify(state.applicant));
        Object.assign(tar, tbrObj);
        tar.uwMedicalId = item.uwMedicalId || "";
        tar.healthyStatus = item.healthyStatus;
        tar.relationToAppnt = "4";
        tar.relationToAppntName = "本人";
      } else {
        tar = item;
        if (item.age) {
          tar.age = item.age;
        }
        if (item.sex !== "") {
          tar.sex = item.sex;
        }
        if (item.birthDate) {
          tar.birthDate = item.birthDate;
        }
      }
      list.push(tar);
    }
    // console.log("getters---3", list);
    return list;
  },
  cont: (state, getters, rootState) => {
    let objs = JSON.parse(JSON.stringify(getters.inputsConfig.payInfo));
    Object.assign(objs, state.payInfo);
    objs.bankAccName = state.applicant.name;
    return objs;
  },
};
const mutations = {
  /* === 受益人 === */
  // 添加
  ADD_ASSURED(state, payload) {
    let objDemo = JSON.parse(JSON.stringify(userModel));
    if (payload && payload.birthDate) {
      Object.assign(objDemo, payload);
    }
    state.bbrList.push(objDemo);
    state.bbrList.splice(state.bbrList.length, 1);
  },
  // 更新
  UPDATE_ASSURED(state, payload) {
    // console.log("UPDATE_ASSURED", payload);
    Object.assign(state.bbrList[payload.index], payload.data);
  },
  // 删除
  DELETE_ASSURED(state, index) {
    state.bbrList.splice(index, 1);
    state.bbrRefresh = true;
    setTimeout(() => {
      state.bbrRefresh = false;
    }, 50);
  },
  SET_BBR_REFRESH(state, payload) {
    state.bbrRefresh = payload;
  },
  // app.vue继续投保回显赋值
  SET_ASSURED(state, payload) {
    state.bbrList = [];
    if (payload && payload.length > 0) {
      payload.forEach((element, index) => {
        state.bbrList.push(JSON.parse(JSON.stringify(userModel)));
        Object.assign(state.bbrList[index], element);
      });
    }
  },
  /* === 受益人-END === */
  UPDATE_APPLICANT(state, payload) {
    Object.assign(state.applicant, payload);
  },
  UPDATE_PAYINFO(state, payload) {
    Object.assign(state.payInfo, payload);
  },
  SET_UNDERWRITE_FLAG(state, payload) {
    state.underwriteFlag = payload;
  },
  SET_INSURE_FLAG(state, payload) {
    state.insureFlag = payload.code || "";
    state.insureMsg = payload.msg || "";
  },
  SET_FORBID_ALTER(state, payload) {
    state.forbidAlter = payload;
  },
  SET_RECALL(state, payload) {
    state.recall = payload;
  },
  SET_BNF_TYPE(state, payload) {
    state.bnfiType = payload;
  },
  SET_MS_CHANNEL(state, payload) {
    state.msChannel = payload;
  },
};
const actions = {
  initAssured({ commit, rootState, state }) {},
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
